package cn.ybk.product.service.impl;

import cn.ybk.basic.service.impl.BaseServiceImpl;
import cn.ybk.basic.util.BaiduAuditUtils;
import cn.ybk.basic.util.CodeGenerateUtils;
import cn.ybk.basic.util.LoginContext;
import cn.ybk.order.domain.OrderProduct;
import cn.ybk.order.mapper.OrderProductMapper;
import cn.ybk.product.domain.Product;
import cn.ybk.product.domain.ProductDetail;
import cn.ybk.product.domain.ProductOnlineAuditLog;
import cn.ybk.product.dto.PurchaseDto;
import cn.ybk.product.mapper.ProductDetailMapper;
import cn.ybk.product.mapper.ProductMapper;
import cn.ybk.product.mapper.ProductOnlineAuditLogMapper;
import cn.ybk.product.service.IProductService;
import cn.ybk.user.domain.Logininfo;
import org.apache.commons.lang3.StringUtils;
import org.omg.CORBA.IRObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 11
 * @since 2023-03-18
 */
@Service
public class ProductServiceImpl extends BaseServiceImpl<Product> implements IProductService {

    @Autowired
    private ProductDetailMapper productDetailMapper;

    @Autowired
    private ProductMapper productMapper;

    @Autowired
    private ProductOnlineAuditLogMapper LogMapper;

    @Autowired
    private OrderProductMapper orderProductMapper;

    // 添加
    @Override
    public void add(Product product) {
        // 添加detail表
        product.setCreatetime(new Date());
        product.setState(0);
        super.add(product);
        ProductDetail productDetail = new ProductDetail();
        ProductDetail detail = product.getDetail();
        productDetail.setIntro(detail.getIntro());
        productDetail.setOrderNotice(detail.getOrderNotice());
        productDetail.setProductId(product.getId());
        productDetailMapper.add(productDetail);
    }

    // 修改
    @Override
    public void update(Product product) {
        super.update(product);
        productDetailMapper.update(product.getDetail());
    }

    // 上架
    @Override
    public void onsale(List<Long> ids) {
        // 获取ids
        List<Product> products =  productMapper.findByState(ids);
        // 如果为空不管
        if (CollectionUtils.isEmpty(products)) {
            return;
        }
        // 下架改为上架，
        // 修改状态
        // 百度ai校验数据
        ArrayList<ProductOnlineAuditLog> productOnlineAuditLogs = new ArrayList<>();
        ArrayList<Long> productId = new ArrayList<>();
        products.forEach(product -> {
            StringBuilder sb = new StringBuilder();
            if (!BaiduAuditUtils.TextCensor(product.getName())) {
                sb.append("服务名违规：").append(",");
            }
            String resources = product.getResources();
            if (StringUtils.isNotBlank(resources)) {
                String[] split = resources.split(",");
                for (int i = 0; i < split.length; i++) {
                    if (!BaiduAuditUtils.ImgCensor(split[i])) {
                        sb.append("服务图片违规");
                    }
                }
            }
            //  审核
            if (sb.length() > 0) {
                ProductOnlineAuditLog log = new ProductOnlineAuditLog();
                log.setAuditTime(new Date());
                // 管理员审核id
                log.setAuditId(-1L);
                log.setNote(sb.toString());
                log.setState(0);
                log.setProductId(product.getId());
                productOnlineAuditLogs.add(log);
                return;
            }
            productId.add(product.getId());
        });
        // 修改状态
        if (!CollectionUtils.isEmpty(productId)) {
            // 批量修改
            productMapper.batchUpdate(productId);
        }
        // 添加日志--只有审核不通过才添加
        if (!CollectionUtils.isEmpty(productOnlineAuditLogs)) {
            // 批量添加
            LogMapper.batchAdd(productOnlineAuditLogs);
        }
    }


    // 下架
    @Override
    public void offsale(List<Long> ids) {
        List<Product> products = productMapper.findByIds(ids);
        if (CollectionUtils.isEmpty(products)) {
            return;
        }
        ArrayList<ProductOnlineAuditLog> productOnlineAuditLogs = new ArrayList<>();
        ArrayList<Long> productId = new ArrayList<>();
        products.forEach(product -> {
            ProductOnlineAuditLog log = new ProductOnlineAuditLog();
            log.setAuditId(-1L);
            log.setProductId(product.getId());
            log.setState(0);
            log.setAuditTime(new Date());
            log.setNote("商品下架！~");
            productOnlineAuditLogs.add(log);
            productId.add(product.getId());
        });
        if (!CollectionUtils.isEmpty(productId)) {
            // 批量修改
            System.out.println(productId);
            productMapper.batchUpdateLog(productId);
        }
        // 添加日志
        if (!CollectionUtils.isEmpty(productOnlineAuditLogs)) {
            // 批量添加
            LogMapper.batchAdd(productOnlineAuditLogs);
        }
    }
    // 删除
    @Override
    public void delete(Long id) {
        ProductDetail detail = productDetailMapper.findByDetail(id);
        productDetailMapper.delete(detail.getId());
        super.delete(id);
    }

    // 服务订单表
    @Override
    public void purchase(PurchaseDto dto) {
        Logininfo logininfo = LoginContext.getLogininfo();
        /*
           lastPayTime  最后支付时间
         */
        OrderProduct orderProduct = new OrderProduct();
        orderProduct.setDigest("你有新的服务订单~");
        orderProduct.setState(0);
        orderProduct.setPrice(dto.getSaleprice());
        orderProduct.setPaySn(CodeGenerateUtils.generateUnionPaySn());

        orderProduct.setLastConfirmTime(new Date(System.currentTimeMillis()+ 2*24*60*60*1000));
        orderProduct.setProductId(dto.getId());
        orderProduct.setUserId(logininfo.getId());
        orderProductMapper.add(orderProduct);
    }


}


















